package com.peking.donations.base.vo;

import com.google.common.base.Strings;
import org.springframework.web.util.HtmlUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YGR on 16/2/15.
 */
public class AjaxResponse {

    public static final String STATE_SUCCESS = "success";

    public static final String STATE_ERROR = "error";

    public static final String STATE_WARN = "warn";

    public static AjaxResponse OK() {
        return new AjaxResponse("OK", AjaxResponse.STATE_SUCCESS);
    }

    public static AjaxResponse OK(Object result) {
        AjaxResponse response = new AjaxResponse("OK", AjaxResponse.STATE_SUCCESS);
        response.setResult(result);
        return response;
    }

    public static AjaxResponse ERROR(String code) {
        return new AjaxResponse(code, AjaxResponse.STATE_ERROR);
    }

    /**
     * 关联错误描述的code
     */
    private String code;

    /**
     * 标识成功或失败
     */
    private String state;

    /**
     * 字段级消息提示，一般用于错误消息的封装，key是字段名，value是描述
     */
    private Map<String, String> messageMap = new HashMap<>();

    /**
     * @param state
     */
    public AjaxResponse(String state) {
        this.state = state;
    }

    /**
     * @param message
     */
    public AjaxResponse(String message, String state) {
        if (!Strings.isNullOrEmpty(message)) {
            message = HtmlUtils.htmlEscape(message);
        }
        this.message = message;
        this.state = state;
    }

    /**
     * @param message
     */
    public AjaxResponse(String code, String message, String state) {
        if (!Strings.isNullOrEmpty(message)) {
            message = HtmlUtils.htmlEscape(message);
        }
        this.code = code;
        this.message = message;
        this.state = state;
    }

    /**
     * 已经格式化好的消息
     */
    private String message;

    /**
     * 需要返回给前端的值，一般form提交情况不用
     */
    private Object result;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (!Strings.isNullOrEmpty(message)) {
            message = HtmlUtils.htmlEscape(message);
        }
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Map<String, String> getMessageMap() {
        return Collections.unmodifiableMap(messageMap);
    }

    public void setMessageMap(Map<String, String> messageMap) {
        if (messageMap != null && !messageMap.isEmpty()) {
            for (String key : messageMap.keySet()) {
                this.addMessageToMap(key, messageMap.get(key));
            }
        }
    }

    public void addMessageToMap(String key, String value) {
        if (!Strings.isNullOrEmpty(key)) {
            key = HtmlUtils.htmlEscape(key);

            if (!Strings.isNullOrEmpty(value)) {
                value = HtmlUtils.htmlEscape(value);
            }
            this.messageMap.put(key, value);
        }
    }
}
